// pages/setting/setting.js
let utils = require("../../utils/util");
Page({
  data: {
    screenBrightness: '获取中',
    SDKVersion: '',
    enableUpdate: true,
    keepscreenon: false,
    setting: {},
    keepscreenon: false
  },

  onShow: function () {
    this.setData({
      keepscreenon: getApp().globalData.keepscreenon
    });
    this.ifDisableUpdate();
    this.getScreenBrightness();
    wx.getStorage({
      key: 'setting',
      success: res => {
        this.setData({
          setting: res.data
        });
      },
      fail: res => {
        this.setData({
          setting: {}
        })
      }
    })
  },
  getScreenBrightness() {
    wx.getScreenBrightness({
      success: res => {
        this.setData({
          screenBrightness: (res.value * 100).toFixed(0)
        });
      },
      fail: res => {
        screenBrightness: "获取失败"
      }
    })
  },
  ifDisableUpdate() {
    let systeminfo = getApp().globalData.systeminfo;
    let SDKVersion = systeminfo.SDKVersion;
    let version = utils.cmpVersion(SDKVersion, '1.9.90')
    if (version >= 0) {
      this.setData({
        SDKVersion,
        enableUpdate: true
      })
    } else {
      this.setData({
        SDKVersion,
        enableUpdate: false
      })
    }
  },
  switchChange(e) {
    let dataset = e.currentTarget.dataset;
    let switchparam = dataset.switchparam;
    let setting = this.data.setting;
    if (switchparam === "forceUpdate") {
      if (this.data.enableUpdate)
        setting[switchparam] = e.detail.value;
      else {
        setting[switchparam] = false;
        wx.showToast({
          title: '基础库版本较低，无法使用该功能'
        })
      }
    } else if (switchparam === "keepscreenon") {
      this.setKeepScreenOn(!this.data.keepscreenon);
      getApp().globalData.keepscreenon = !this.data.keepScreenOn;
    } else {
      setting[switchparam] = e.detail.value;
    }
    this.setData({
      setting
    });
    wx.setStorage({
      data: setting,
      key: 'setting',
      success: () => {

      }
    })
  },
  setKeepScreenOn(b) {
    wx.setKeepScreenOn({
      keepScreenOn: b,
      success: res => {
        this.setData({
          keepscreenon: b
        });
      }
    })
  },
  getHCEState() {
    wx.showLoading({
      title: '检测中'
    })
    wx.getHCEState({
      success: res => {
        wx.hideLoading();
        wx.showModal({
          cancelColor: 'blue',
          title: "检测结果",
          content: '该设备支持NFC功能'
        })
      },
      fail: res => {
        wx.hideLoading();
        wx.showModal({
          cancelColor: 'blue',
          title: "检测结果",
          content: '该设备不支持NFC功能',
          showCancel: false,
          success: res => console.log("success"),
          fail: res => console.log("fail"),
          confirmText: '朕知道了',
          confirmColor: '#40a7e7'
        })
      }
    })
  },
  getsysteminfo() {
    wx.navigateTo({
      url:  "/pages/systeminfo/systeminfo"
    })
  },
  screenBrightnessChanging(e) {
    this.setScreenBrightness(e.detail.value);
  },

  setScreenBrightness(val) {
    wx.setScreenBrightness({
      value: val / 100,
      success: res => {
        this.setData({
          "screenBrightness": val
        })
      }
    })
  },
  removeStorage(e) {
    let dataset = e.currentTarget.dataset;
    let type = dataset.type;
    if (type === "setting") {
      wx.showModal({
        cancelText: "容朕想想",
        title: "提示",
        content: "确认要初始化设置",
        success: res => {
          if (res.confirm) {
            wx.removeStorage({
              key: "setting",
              success: res => {
                wx.showToast({
                  title: '设置已初始化'
                })
                this.setData({
                  setting: {}
                });
              }
            });
          }
        }
      })
    } else {
      wx.showModal({
        title: "提示",
        content: "确认要清除所有数据么?",
        cancelText: "容朕想想",
        success: res => {
          if (res.confirm) {
            wx.clearStorage({
              success: res => {
               wx.showToast({
                 title: '数据已清除'
               });
               this.setData({
                 setting:{}
               });

              }
            })
          }
        }
      })
    }
  }
})